unit Concentrations;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, OptionsUnit;

type
  TfrmConcentrations = class(TForm)
    tbCA: TTrackBar;
    tbCB: TTrackBar;
    edCA: TEdit;
    edCB: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    btnSet: TButton;
    procedure tbCAChange(Sender: TObject);
    procedure tbCBChange(Sender: TObject);
    procedure edCAChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure edCBChange(Sender: TObject);
    procedure btnSetClick(Sender: TObject);
  private
    { Private declarations }
  public
    ProcOpt: TProcOpt;
    fEdALock, fEdBLock: boolean;
    fTbLock: boolean;
  end;

var
  frmConcentrations: TfrmConcentrations;

implementation

{$R *.dfm}

procedure TfrmConcentrations.tbCAChange(Sender: TObject);
begin
  if fEdALock then
  begin
    fEdALock := false;
    exit;
  end;
  if ((100 - tbCA.Position) + (100 - tbCB.Position)) > 100 then
  begin
    tbCA.Position := Round((1 - StrToFloat(edCA.Text))*100);
    exit;
  end;
  edCA.Text := FloatToStr(1 - tbCA.Position/100);
end;

procedure TfrmConcentrations.tbCBChange(Sender: TObject);
begin
  if fEdBLock then
  begin
    fEdBLock := false;
    exit;
  end;
  if ((100 - tbCA.Position) + (100 - tbCB.Position)) > 100 then
  begin
    tbCB.Position := Round((1 - StrToFloat(edCB.Text))*100);
    exit;
  end;
  edCB.Text := FloatToStr(1 - tbCB.Position/100);
end;

procedure TfrmConcentrations.edCAChange(Sender: TObject);
var
  B: double;
  A: double;
begin
  fEdALock := true;
  try
    B := StrToFloat(edCB.Text);
    A := StrToFloat(edCA.Text);
  except
    edCA.Text := FloatToStr(1 - tbCA.Position/100);
    exit;
  end;
  if A + B > 1 then
  begin
    edCA.Text := FloatToStr(1 - StrToFloat(edCB.Text));
    exit;
  end;
  tbCA.Position := Round((1 - A)*100);
end;

procedure TfrmConcentrations.FormShow(Sender: TObject);
var
  CA, CB: extended;
begin
  fEdALock := false;
  fEdBLock := false;
  CA := (ProcOpt as TTubeReactProcOpt).CA;
  CB := (ProcOpt as TTubeReactProcOpt).CB;
  edCA.Text := FloatToStr(CA);
  edCB.Text := FloatToStr(CB);
  tbCA.Position := Round((1 - CA)*100);
  tbCB.Position := Round((1 - CB)*100);
end;

procedure TfrmConcentrations.edCBChange(Sender: TObject);
var
  B: double;
  A: double;
begin
  fEdBLock := true;
  try
    B := StrToFloat(edCB.Text);
    A := StrToFloat(edCA.Text);
  except
    edCB.Text := FloatToStr(1 - tbCB.Position/100);
    exit;
  end;
  if A + B > 1 then
  begin
    edCB.Text := FloatToStr(1 - StrToFloat(edCA.Text));
    exit;
  end;
  tbCB.Position := Round((1 - B)*100);
end;

procedure TfrmConcentrations.btnSetClick(Sender: TObject);
begin
  (ProcOpt as TTubeReactProcOpt).CA := StrToFloat(edCA.Text);
  (ProcOpt as TTubeReactProcOpt).CB := StrToFloat(edCB.Text);
end;

end.
